package com.cat.chapter037;

/**
 * @author cat
 * @description https://leetcode.cn/problems/validate-binary-search-tree/
 * @create 2025/6/24 17:02
 * @since JDK17
 */

public class Solution04 {
    Long max = Long.MIN_VALUE;
    Long min = Long.MAX_VALUE;
    public boolean isValidBST(TreeNode root) {
        if (root == null) {
            max = Long.MIN_VALUE;
            min = Long.MAX_VALUE;
            return true;
        }
        max = Long.max(max, root.val);
        min = Long.min(min, root.val);
        boolean l =  isValidBST(root.left);  //
        Long lmax = max;
        Long lmin = min;
        boolean r =  isValidBST(root.right); // 往右边找
        Long rmax = max;
        Long rmin = min;
        max = Long.max(rmax, root.val);
        min = Long.min(lmin, root.val);
        return r && l && lmax < root.val && root.val < rmin;
    }
}
